package com.itheima.customer.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.itheima.customer.dao.ILinkManDao;
import com.itheima.customer.domain.LinkMan;

@Repository("linkManDao")
public class LinkManDaoImpl implements ILinkManDao {
	
	@Autowired
	private HibernateTemplate hibernateTemplate;
	
	@Override
	public void addLinkMan(LinkMan linkman) {
		hibernateTemplate.save(linkman);
	}

	@Override
	public List<LinkMan> findAll(DetachedCriteria dc , int firstResult,  int maxResults) {
		dc.setProjection(null);
		return (List<LinkMan>) hibernateTemplate.findByCriteria(dc,firstResult,maxResults);
	}

	@Override
	public void remove(LinkMan linkman) {
		
		 hibernateTemplate.delete(linkman);
		
	}

	@Override
	public LinkMan findById(Long lkmId) {
	
		return  hibernateTemplate.get(LinkMan.class, lkmId);
	}

	@Override
	public void update(LinkMan linkman) {
		hibernateTemplate.update(linkman);
	}

	@Override
	public int findTotalRecords(DetachedCriteria dc) {
		
//		dc.setProjection(Projections.rowCount());
		//QBC的统计查询
		dc.setProjection(Projections.count("lkmId"));//select * from cst_customer把它转成select count(*) from cst_customer
		 List<Long> list = (List<Long>) hibernateTemplate.findByCriteria(dc);
		 return list.isEmpty()?0:list.get(0).intValue();
	}
}
